﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SuperMap.Data;
using System.IO;
using SuperMap.Analyst.SpatialAnalyst;
using SuperMap.Mapping;
using SuperMap.Desktop;

namespace SuperMap.HB.ThemeWork
{
    public partial class RiverControl : UserControl
    {
        private const string m_DataSourceName = "河流数据";
        private Datasource m_DataSource = Desktop.Application.ActiveApplication.Workspace.Datasources[m_DataSourceName];

        private RiverInfo m_RiverInfo = new RiverInfo();
        private DataSet m_WaterDataset;
        private string m_WaterSheet = "";
        private string m_WaterDatasetName = "";
        private string m_WaterCode = "";
        private string m_WaterQField = "";

        public RiverControl()
        {
            InitializeComponent();

            groupBox1.Paint += groupBox_Paint;
            groupBox2.Paint += groupBox_Paint;
            groupBox3.Paint += groupBox_Paint;
        }

        private void groupBox_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                GroupBox gBox = (GroupBox)sender;

                e.Graphics.Clear(gBox.BackColor);
                e.Graphics.DrawString(gBox.Text, gBox.Font, Brushes.Black, 10, 1);
                var vSize = e.Graphics.MeasureString(gBox.Text, gBox.Font);
                e.Graphics.DrawLine(Pens.Black, 1, vSize.Height / 2, 8, vSize.Height / 2);
                e.Graphics.DrawLine(Pens.Black, vSize.Width + 8, vSize.Height / 2, gBox.Width - 2, vSize.Height / 2);
                e.Graphics.DrawLine(Pens.Black, 1, vSize.Height / 2, 1, gBox.Height - 2);
                e.Graphics.DrawLine(Pens.Black, 1, gBox.Height - 2, gBox.Width - 2, gBox.Height - 2);
                e.Graphics.DrawLine(Pens.Black, gBox.Width - 2, vSize.Height / 2, gBox.Width - 2, gBox.Height - 2);
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void RiverControl_Load(object sender, EventArgs e)
        {
            try
            {
                m_RiverInfo.Radiuses = new Dictionary<Geometry, double>();
                cmb_SelectData.Items.Clear();
                cmb_WaterRegion.Items.Clear();
                cmb_SelectExistExcel.Items.Clear();
                foreach (Dataset dt in m_DataSource.Datasets)
                {
                    if (dt.Type == DatasetType.Line)
                    {
                        cmb_SelectData.Items.Add(dt.Name);
                    }
                    if (dt.Type == DatasetType.Region)
                    {
                        cmb_WaterRegion.Items.Add(dt.Name);
                    }
                    if (dt.Type == DatasetType.Tabular)
                    {
                        cmb_SelectExistExcel.Items.Add(dt.Name);
                    }
                }
                if (cmb_SelectData.Items.Count > 0)
                {
                    cmb_SelectData.SelectedIndex = 0;
                }
                if (cmb_WaterRegion.Items.Count > 0)
                {
                    cmb_WaterRegion.SelectedIndex = 0;
                }
                if (cmb_SelectExistExcel.Items.Count > 0)
                {
                    cmb_SelectExistExcel.SelectedIndex = 0;
                }

                cmb_SelectMap.Items.Clear();
                foreach (string name in Desktop.Application.ActiveApplication.Workspace.Maps)
                {
                    cmb_SelectMap.Items.Add(name);
                }
                if (cmb_SelectMap.Items.Count > 0)
                {
                    cmb_SelectMap.SelectedIndex = 0;
                    //groupBox1.Enabled = true;
                    //groupBox2.Enabled = true;
                    groupBox3.Enabled = true;
                    rdo_NewRiverRegion.Enabled = true;
                    rdo_SelectRiverRegion.Enabled = true;
                }
                else
                {
                    //groupBox1.Enabled = false;
                    //groupBox2.Enabled = false;
                    groupBox3.Enabled = false;
                    rdo_NewRiverRegion.Enabled = false;
                    rdo_SelectRiverRegion.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void rdo_SelectData_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rdo_SelectData.Checked)
                {
                    cmb_SelectData.Visible = true;
                    txt_SelectPath.Visible = false;
                    btn_SelectPath.Visible = false;
                }
                if (rdo_ImportData.Checked)
                {
                    cmb_SelectData.Visible = false;
                    txt_SelectPath.Visible = true;
                    btn_SelectPath.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void cmb_SelectData_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                m_RiverInfo.DatasetName = cmb_SelectData.Text;
                cmb_TypeField.Items.Clear();
                DatasetVector dtv = (m_DataSource.Datasets[cmb_SelectData.Text] as DatasetVector);
                foreach (FieldInfo info in dtv.FieldInfos)
                {
                    if (!info.Name.ToLower().Contains("sm"))
                    {
                        cmb_TypeField.Items.Add(info.Name);
                    }
                }
                if (cmb_TypeField.Items.Count > 0)
                {
                    cmb_TypeField.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void btn_SelectPath_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog
                {
                    Filter = "Shapefile文件|*.shp",
                    Multiselect = false
                };
                if (open.ShowDialog() == DialogResult.OK)
                {
                    string filepath = open.FileName;
                    txt_SelectPath.Text = filepath;
                    string datasetName = m_DataSource.Datasets.GetAvailableDatasetName((new FileInfo(filepath)).Name.Split('.')[0]);
                    m_RiverInfo.DatasetName = datasetName;
                    ImportDataManager import = new ImportDataManager();
                    import.Import(m_DataSourceName, datasetName, open.FileName);
                    import.OnImportFinished += Import_OnImportFinished;
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void Import_OnImportFinished(object sender, EventArgs e)
        {
            try
            {
                cmb_TypeField.Items.Clear();
                DatasetVector dtv = (m_DataSource.Datasets[m_RiverInfo.DatasetName] as DatasetVector);
                foreach (FieldInfo info in dtv.FieldInfos)
                {
                    if (!info.Name.ToLower().Contains("sm"))
                    {
                        cmb_TypeField.Items.Add(info.Name);
                    }
                }
                if (cmb_TypeField.Items.Count > 0)
                {
                    cmb_TypeField.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void cmb_TypeField_SelectedIndexChanged(object sender, EventArgs e)
        {
            Recordset objRt = null;

            try
            {
                objRt = (m_DataSource.Datasets[m_RiverInfo.DatasetName] as DatasetVector).GetRecordset(false, CursorType.Static);
                m_RiverInfo.Radiuses.Clear();
                while (!objRt.IsEOF)
                {
                    if (objRt.GetFieldValue(cmb_TypeField.Text) != null)
                    {
                        m_RiverInfo.Radiuses.Add(objRt.GetGeometry(), Convert.ToDouble(objRt.GetFieldValue(cmb_TypeField.Text)));
                    }

                    objRt.MoveNext();
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
            finally
            {
                objRt?.Close();
                objRt?.Dispose();
            }
        }

        private void txt_Radius_Leave(object sender, EventArgs e)
        {
            try
            {
                m_RiverInfo.Radius = Convert.ToDouble(txt_Radius.Text.Trim());
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void chk_UniqueRadius_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                txt_Radius.Enabled = chk_UniqueRadius.Checked;
                txt_Scale.Enabled = !chk_UniqueRadius.Checked;
                cmb_TypeField.Enabled = !chk_UniqueRadius.Checked;
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            try
            {
                MessageTip tip = new MessageTip();
                tip.ShowTip();
                if (chk_UniqueRadius.Checked)
                {
                    DatasetVector line = m_DataSource.Datasets[m_RiverInfo.DatasetName] as DatasetVector;
                    DatasetVectorInfo vectorInfo = new DatasetVectorInfo
                    {
                        Type = DatasetType.Region,
                        Name = m_DataSource.Datasets.GetAvailableDatasetName(m_RiverInfo.DatasetName + "_缓冲面")
                    };
                    DatasetVector result = m_DataSource.Datasets.Create(vectorInfo);
                    result.PrjCoordSys = line.PrjCoordSys;

                    //添加水质类型固定字段
                    result.FieldInfos.Add(new FieldInfo { Name = "JB2015", Caption = "专题图类型", Type = FieldType.Text });
                    result.FieldInfos.Add(new FieldInfo { Name = "SYJCD", Caption = "上游监测点编号", Type = FieldType.Int32 });
                    result.FieldInfos.Add(new FieldInfo { Name = "XYJCD", Caption = "下游监测点编号", Type = FieldType.Int32 });
                    result.FieldInfos.Add(new FieldInfo { Name = "XZJD", Caption = "河流走向（从左到右为0，从下往上为90）", Type = FieldType.Text });

                    BufferAnalystParameter par = new BufferAnalystParameter
                    {
                        EndType = BufferEndType.Round,
                        RadiusUnit = BufferRadiusUnit.Meter,
                        LeftDistance = Convert.ToDouble(txt_Radius.Text.Trim()),
                        RightDistance = Convert.ToDouble(txt_Radius.Text.Trim()),
                        SemicircleLineSegment = 100
                    };
                    if (BufferAnalyst.CreateBuffer(line, result, par, false, true))
                    {
                        tip.HideTip();
                        Desktop.Application.ActiveApplication.MessageBox.Show("生成缓冲区成功！\n请打开属性表添加【上游监测点编号】、【下游监测点编号】、【河流走向】信息!");
                    }
                    else
                    {
                        tip.HideTip();
                        Desktop.Application.ActiveApplication.MessageBox.Show("生成缓冲区失败！");
                    }
                }
                else
                {
                    Recordset objRt = null;
                    Recordset objNewRt = null;

                    try
                    {
                        DatasetVectorInfo vectorInfo = new DatasetVectorInfo
                        {
                            Type = DatasetType.Region,
                            Name = m_DataSource.Datasets.GetAvailableDatasetName(m_RiverInfo.DatasetName + "_缓冲面")
                        };
                        DatasetVector result = m_DataSource.Datasets.Create(vectorInfo);
                        result.PrjCoordSys = m_DataSource.Datasets[m_RiverInfo.DatasetName].PrjCoordSys;
                        objNewRt = result.GetRecordset(true, CursorType.Dynamic);

                        objRt = (m_DataSource.Datasets[m_RiverInfo.DatasetName] as DatasetVector).GetRecordset(false, CursorType.Static);
                        m_RiverInfo.Radiuses.Clear();
                        while (!objRt.IsEOF)
                        {
                            GeoLine line = objRt.GetGeometry() as GeoLine;
                            BufferAnalystParameter par = new BufferAnalystParameter
                            {
                                EndType = BufferEndType.Round,
                                LeftDistance = Convert.ToDouble(txt_Radius.Text.Trim()),
                                RightDistance = Convert.ToDouble(txt_Radius.Text.Trim()),
                                RadiusUnit = BufferRadiusUnit.Meter,
                                SemicircleLineSegment = 100
                            };

                            bool b = objNewRt.AddNew(geometry: BufferAnalystGeometry.CreateBuffer(geometry: line, bufferAnalystParameter: par, prjCoordSys: m_DataSource.Datasets[m_RiverInfo.DatasetName].PrjCoordSys));
                            bool bb = objNewRt.Update();

                            objRt.MoveNext();
                        }
                        tip.HideTip();
                        Desktop.Application.ActiveApplication.MessageBox.Show("生成缓冲区成功！\n请打开属性表添加【上游监测点编号】、【下游监测点编号】、【河流走向】信息!");
                    }
                    catch (Exception ex)
                    {
                        Log.OutputBox(ex);
                        tip.HideTip();
                        Desktop.Application.ActiveApplication.MessageBox.Show("生成缓冲区失败！");
                    }
                    finally
                    {
                        objRt?.Close();
                        objRt?.Dispose();

                        objNewRt?.Close();
                        objNewRt?.Dispose();
                    }
                }

                cmb_WaterRegion.Items.Clear();
                foreach (Dataset dt in m_DataSource.Datasets)
                {
                    if (dt.Type == DatasetType.Region)
                    {
                        cmb_WaterRegion.Items.Add(dt.Name);
                    }
                }
                if (cmb_WaterRegion.Items.Count > 0)
                {
                    cmb_WaterRegion.SelectedIndex = 0;
                    btn_WaterThemeOK.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void btn_SetStyle_Click(object sender, EventArgs e)
        {
            try
            {
                
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void btn_SelectWaterQFile_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog
                {
                    Filter = "所有文件|*.*|Shapefile文件|*.shp|Excel文件|*.xlsx|Excel文件|*.xls",
                    Multiselect = false
                };
                if (open.ShowDialog() == DialogResult.OK)
                {
                    txt_WaterQPath.Text = open.FileName;
                    ExcelHelper excel = new ExcelHelper();
                    m_WaterDataset = excel.FromFile(open.FileName);
                    if (CheckData.ConfirmData(m_WaterDataset))
                    {
                        cmb_SelectSheet.Items.Clear();
                        foreach (DataTable table in m_WaterDataset.Tables)
                        {
                            cmb_SelectSheet.Items.Add(table.TableName);
                        }
                        if (cmb_SelectSheet.Items.Count > 0)
                        {
                            cmb_SelectSheet.SelectedIndex = 0;
                        }
                    }
                    else
                    {
                        Desktop.Application.ActiveApplication.MessageBox.Show("数据表结构不适用，请修正数据后重试！");
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void cmb_WaterCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                m_WaterCode = cmb_WaterCode.Text;
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void cmb_WaterQField_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                m_WaterQField = cmb_WaterQField.Text;
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void cmb_SelectSheet_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                m_WaterSheet = cmb_SelectSheet.Text;
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void btn_Import_Click(object sender, EventArgs e)
        {
            try
            {
                MessageTip tip = new MessageTip();
                tip.ShowTip();
                foreach (DataTable table in m_WaterDataset.Tables)
                {
                    if (!table.TableName.Equals(m_WaterSheet))
                    {
                        continue;
                    }
                    DatasetVectorInfo info = new DatasetVectorInfo
                    {
                        Type = DatasetType.Tabular,
                        Name = m_DataSource.Datasets.GetAvailableDatasetName(m_WaterSheet),
                    };
                    m_WaterDatasetName = info.Name;
                    DatasetVector objDtV = m_DataSource.Datasets.Create(info);
                    objDtV.Open();

                    List<FieldInfo> lstColumns = new List<FieldInfo>();
                    cmb_WaterCode.Items.Clear();
                    cmb_WaterQField.Items.Clear();
                    foreach (DataColumn column in table.Columns)
                    {
                        if (column.ColumnName.ToLower().Contains("sm"))
                        {
                            continue;
                        }
                        FieldInfo fieldInfo = new FieldInfo { Name = column.ColumnName, Caption = column.ColumnName, Type = FieldType.Text };
                        objDtV.FieldInfos.Add(fieldInfo);
                        lstColumns.Add(fieldInfo);
                        cmb_WaterCode.Items.Add(fieldInfo.Name);
                        cmb_WaterQField.Items.Add(fieldInfo.Name);
                    }
                    if (cmb_WaterCode.Items.Count > 0)
                    {
                        cmb_WaterCode.SelectedIndex = 0;
                    }
                    if (cmb_WaterQField.Items.Count > 0)
                    {
                        cmb_WaterQField.SelectedIndex = 0;
                    }

                    Recordset objRt = null;

                    try
                    {
                        objRt = objDtV.GetRecordset(true, CursorType.Dynamic);

                        Recordset.BatchEditor editor = objRt.Batch;
                        editor.MaxRecordCount = 50;
                        editor.Begin();

                        foreach (DataRow row in table.Rows)
                        {
                            Dictionary<string, object> dic = new Dictionary<string, object>();
                            foreach (FieldInfo fieldInfo in lstColumns)
                            {
                                dic.Add(fieldInfo.Name, row[fieldInfo.Name]);
                            }
                            objRt.AddNew(null, dic);
                        }

                        editor.Update();

                    }
                    catch (Exception ex)
                    {
                        Log.OutputBox(ex);
                    }
                    finally
                    {
                        objRt?.Close();
                        objRt?.Dispose();
                    }
                }

                tip.HideTip();
                Desktop.Application.ActiveApplication.MessageBox.Show("导入成功！");
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void btnWaterOK_Click(object sender, EventArgs e)
        {
            Recordset objRtPoint = null;
            Recordset objRtWaterRegion = null;

            try
            {
                MessageTip tip = new MessageTip();
                tip.ShowTip();
                
                objRtWaterRegion = (m_DataSource.Datasets[cmb_WaterRegion.Text] as DatasetVector).GetRecordset(false, CursorType.Dynamic);
                DatasetVector objDtVPoint = m_DataSource.Datasets[cmb_SelectExistExcel.Text.Trim()] as DatasetVector;

                bool dataExist = false;

                objRtWaterRegion.MoveFirst();
                while (!objRtWaterRegion.IsEOF)
                {
                    string UpValue = "";
                    string DownValue = "";
                    if (objRtWaterRegion.GetFieldValue("SYJCD") != null)
                    {
                        objRtPoint = objDtVPoint.Query(m_WaterCode + " = " + objRtWaterRegion.GetFieldValue("SYJCD").ToString(), CursorType.Static);
                        if (objRtPoint.RecordCount > 0)
                        {
                            switch (objRtPoint.GetFieldValue(m_WaterQField).ToString())
                            {
                                case "—":
                                    UpValue = "0";
                                    break;
                                case "Ⅰ类":
                                    UpValue = "1";
                                    break;
                                case "Ⅱ类":
                                    UpValue = "2";
                                    break;
                                case "Ⅲ类":
                                    UpValue = "3";
                                    break;
                                case "Ⅳ类":
                                    UpValue = "4";
                                    break;
                                case "Ⅴ类":
                                    UpValue = "5";
                                    break;
                                case "劣Ⅴ类":
                                    UpValue = "6";
                                    break;
                            }
                        }
                    }
                    else
                    {
                        UpValue = "0";
                    }

                    if (objRtWaterRegion.GetFieldValue("XYJCD") != null)
                    {
                        objRtPoint = objDtVPoint.Query(m_WaterCode + " = " + objRtWaterRegion.GetFieldValue("XYJCD").ToString(), CursorType.Static);
                        if (objRtPoint.RecordCount > 0)
                        {
                            switch (objRtPoint.GetFieldValue(m_WaterQField).ToString())
                            {
                                case "—":
                                    DownValue = "0";
                                    break;
                                case "Ⅰ类":
                                    DownValue = "1";
                                    break;
                                case "Ⅱ类":
                                    DownValue = "2";
                                    break;
                                case "Ⅲ类":
                                    DownValue = "3";
                                    break;
                                case "Ⅳ类":
                                    DownValue = "4";
                                    break;
                                case "Ⅴ类":
                                    DownValue = "5";
                                    break;
                                case "劣Ⅴ类":
                                    DownValue = "6";
                                    break;
                            }
                        }
                    }
                    else
                    {
                        DownValue = "0";
                    }

                    objRtWaterRegion.Edit();
                    dataExist = objRtWaterRegion.SetFieldValue("JB2015", UpValue + DownValue);
                    objRtWaterRegion.Update();

                    objRtWaterRegion.MoveNext();
                }

                objRtWaterRegion.MoveFirst();
                while (!objRtWaterRegion.IsEOF)
                {
                    string Direction = "";
                    if (objRtWaterRegion.GetFieldValue("XZJD") != null)
                    {
                        switch (objRtWaterRegion.GetFieldValue("XZJD").ToString())
                        {
                            case "90":
                                Direction = "XS";
                                break;
                            case "180":
                                Direction = "YZ";
                                break;
                            case "270":
                                Direction = "SX";
                                break;
                        }
                    }

                    if (objRtWaterRegion.GetFieldValue("JB2015") != null)
                    {
                        objRtWaterRegion.Edit();
                        dataExist = objRtWaterRegion.SetFieldValue("JB2015", objRtWaterRegion.GetFieldValue("JB2015").ToString() + Direction);
                        objRtWaterRegion.Update();
                    }

                    objRtWaterRegion.MoveNext();
                }

                if (!dataExist)
                {
                    Desktop.Application.ActiveApplication.MessageBox.Show("未匹配到水质数据，请打开属性表添加【上游监测点编号】、【下游监测点编号】、【河流走向】信息!");
                }

                tip.HideTip();
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
            finally
            {
                objRtPoint?.Close();
                objRtPoint?.Dispose();

                objRtWaterRegion?.Close();
                objRtWaterRegion?.Dispose();
            }
        }

        private void btn_WaterThemeOK_Click(object sender, EventArgs e)
        {
            try
            {
                btnWaterOK_Click(null, null);

                ThemeUnique themeUnique = new ThemeUnique()
                {
                    UniqueExpression = "JB2015",
                    IsDefaultStyleVisible = false
                };
                StreamReader sr = new StreamReader(AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.Length - 4) + @"Templates\ThemeWork\Theme\河流水质模板.xml");
                themeUnique.FromXML(sr.ReadToEnd());

                Desktop.Application.ActiveApplication.MainForm.FormManager.CloseAll(false);

                IFormMap formMap = Desktop.Application.ActiveApplication.CreateMapWindow();
                formMap.MapControl.Map.Open(cmb_SelectMap.Text);
                formMap.MapControl.Map.Layers.Add(m_DataSource.Datasets[cmb_WaterRegion.Text], themeUnique, true);
                formMap.MapControl.Map.ViewEntire();
                bool exist = false;
                foreach (string map in Desktop.Application.ActiveApplication.Workspace.Maps)
                {
                    if (map.Equals(cmb_SelectMap.Text.Trim()))
                    {
                        exist = true;
                        break;
                    }
                }
                if (exist)
                {
                    Desktop.Application.ActiveApplication.Workspace.Maps.SetMapXML(cmb_SelectMap.Text.Trim(), formMap.MapControl.Map.ToXML());
                }
                else
                {
                    Desktop.Application.ActiveApplication.Workspace.Maps.Add(cmb_SelectMap.Text.Trim(), formMap.MapControl.Map.ToXML());
                }
                if (Desktop.Application.ActiveApplication.Workspace.Save())
                {
                    Desktop.Application.ActiveApplication.MessageBox.Show("保存成功！");
                }
                else
                {
                    Desktop.Application.ActiveApplication.MessageBox.Show("保存失败！");
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void rdo_SelectRiverRegion_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                RadioButton rdo = sender as RadioButton;
                switch (rdo.Name)
                {
                    case "rdo_SelectRiverRegion":
                        groupBox1.Enabled = false;
                        groupBox2.Enabled = false;
                        break;
                    case "rdo_NewRiverRegion":
                        groupBox1.Enabled = true;
                        groupBox2.Enabled = true;
                        break;
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void cmb_WaterRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                btn_WaterThemeOK.Enabled = true;
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void rdo_SelectTable_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                switch ((sender as RadioButton).Name)
                {
                    case "rdo_SelectTable":
                        cmb_SelectExistExcel.Enabled = true;
                        btn_SelectWaterQFile.Enabled = false;
                        btn_Import.Enabled = false;
                        txt_WaterQPath.Enabled = false;
                        cmb_SelectSheet.Enabled = false;
                        break;
                    case "rdo_ImportExcel":
                        cmb_SelectExistExcel.Enabled = false;
                        btn_SelectWaterQFile.Enabled = true;
                        btn_Import.Enabled = true;
                        txt_WaterQPath.Enabled = true;
                        cmb_SelectSheet.Enabled = true;

                        cmb_WaterCode.Items.Clear();
                        cmb_WaterQField.Items.Clear();
                        break;
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }

        private void cmb_SelectExistExcel_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DatasetVector objDtV = m_DataSource.Datasets[cmb_SelectExistExcel.Text.Trim()] as DatasetVector;
                cmb_WaterCode.Items.Clear();
                cmb_WaterQField.Items.Clear();
                foreach (FieldInfo info in objDtV.FieldInfos)
                {
                    if (!info.Name.ToLower().Contains("sm"))
                    {
                        cmb_WaterCode.Items.Add(info.Name);
                        cmb_WaterQField.Items.Add(info.Name);
                    }
                }
                if (cmb_WaterCode.Items.Count > 0)
                {
                    cmb_WaterCode.SelectedIndex = 0;
                }
                if (cmb_WaterQField.Items.Count > 0)
                {
                    cmb_WaterQField.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                Log.OutputBox(ex);
            }
        }
    }
}
